//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "LatticeRule.h"
using namespace Cephei::QL::Math::Randomnumbers;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Randomnumbers::CLatticeRule::CLatticeRule (boost::shared_ptr<QuantLib::LatticeRule>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLatticeRule = NULL;
#endif
	_ppLatticeRule = &childNative;
    
}
Cephei::QL::Math::Randomnumbers::CLatticeRule::CLatticeRule (QuantLib::LatticeRule& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLatticeRule = NULL;
#endif
	_ppLatticeRule = new boost::shared_ptr<QuantLib::LatticeRule> (&childNative);
    
    _LatticeRuleOwner = owner;
    
}

Cephei::QL::Math::Randomnumbers::CLatticeRule::CLatticeRule (CLatticeRule^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLatticeRule = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLatticeRule = new boost::shared_ptr<QuantLib::LatticeRule> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Randomnumbers::CLatticeRule::CLatticeRule (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLatticeRule = NULL;
#endif
	if (!t->IsSubclassOf(CLatticeRule::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Randomnumbers::CLatticeRule::CLatticeRule (QuantLib::Handle<QuantLib::LatticeRule>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLatticeRule = &childNative;
	_ppLatticeRule = &static_cast<boost::shared_ptr<QuantLib::LatticeRule>>(childNative.currentLink());
    
    _LatticeRuleOwner = owner;
}
Cephei::QL::Math::Randomnumbers::CLatticeRule::CLatticeRule (QuantLib::Handle<QuantLib::LatticeRule> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLatticeRule = &childNative;
	_ppLatticeRule = &static_cast<boost::shared_ptr<QuantLib::LatticeRule>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Randomnumbers::CLatticeRule::CLatticeRule (QuantLib::LatticeRule childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLatticeRule = NULL;
#endif
	_ppLatticeRule = new boost::shared_ptr<QuantLib::LatticeRule> (new QuantLib::LatticeRule (childNative));
    
}
#endif

Cephei::QL::Math::Randomnumbers::CLatticeRule::~CLatticeRule ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLatticeRule != NULL)
    {
	    delete _ppLatticeRule;
        _ppLatticeRule = NULL;
    }
}
Cephei::QL::Math::Randomnumbers::CLatticeRule::!CLatticeRule ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLatticeRule != NULL)
    {
	    delete _ppLatticeRule;
    }
}
QuantLib::LatticeRule& Cephei::QL::Math::Randomnumbers::CLatticeRule::GetReference ()
{
    if (_ppLatticeRule == NULL) throw REFNEW NativeNullException ();
	return **_ppLatticeRule;
}
boost::shared_ptr<QuantLib::LatticeRule>& Cephei::QL::Math::Randomnumbers::CLatticeRule::GetShared ()
{
    if (_ppLatticeRule == NULL) throw REFNEW NativeNullException ();
	return *_ppLatticeRule;
}
QuantLib::LatticeRule* Cephei::QL::Math::Randomnumbers::CLatticeRule::GetPointer ()
{
    if (_ppLatticeRule == NULL) throw REFNEW NativeNullException ();
	return &**_ppLatticeRule;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LatticeRule>& Cephei::QL::Math::Randomnumbers::CLatticeRule::GetHandle ()
{
	if (_phLatticeRule == NULL)
	{
		_phLatticeRule = new Handle<QuantLib::LatticeRule> (*_ppLatticeRule);
	}
	return *_phLatticeRule;
}
#endif
bool Cephei::QL::Math::Randomnumbers::CLatticeRule::HasNative () 
{
	return (_ppLatticeRule != NULL);
}

Cephei::QL::Math::Randomnumbers::ILatticeRule^ Cephei::QL::Math::Randomnumbers::CLatticeRule::GetRule (QL::Math::Randomnumbers::LatticeRule::typeEnum name, Cephei::Core::IVector<Double>^ Z, Int32 N)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::LatticeRule::type _name = (QuantLib::LatticeRule::type)name ;
        Z->Lock();
        INativeVector<Double>^ _NCIZ = Z->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCZ = safe_cast<CDoubleVector^>(_NCIZ);
        std::vector<QuantLib::Real>& _Z = static_cast<std::vector<QuantLib::Real>&> (_NCZ->GetReference ());
        QuantLib::Integer _N = (QuantLib::Integer)ValueHelper::Convert (N); //a
    	(*_ppLatticeRule)->getRule ( _name,  _Z,  _N );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (Z != nullptr) Z->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

